package com.mm.dss.demo.adapter;

import android.content.Context;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.mm.dss.demo.R;

import java.io.File;
import java.util.List;

/**
 * Created by Lenovo on 2018/6/14.
 */

public class Myadapter extends  RecyclerView.Adapter<Myadapter.ViewHolder> implements View.OnClickListener{
    List<String> list;
    Context context;
    private onRecyclerItemClickerListener mListener;
    public Myadapter(List<String> list, Context context){
        this.list=list;
        this.context=context;
    }
    /*      *//**
     * 增加点击监听
     */
      public void setItemListener(onRecyclerItemClickerListener mListener) {
          this.mListener = mListener;
      }
    @Override
    public Myadapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.itme_recyimgview,null);
          Myadapter.ViewHolder vh=new Myadapter.ViewHolder(view);
          view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(Myadapter.ViewHolder holder, final int position) {
        File file = new File(list.get(position));
        Log.i("图片地址",file+"?"+ Environment.getExternalStorageDirectory()+"?"+list.get(position));
        Glide.with(context).load(file).into(holder.img);
/*        Bitmap bitmap = HelpUtils.compressBitmap(list.get(position), 600, 1024,
                30);//图片转bitmap*/
/*        LogUtil.e("base64", Utils.bitmapToBase64(bitmap));//转64
        // Utils.imageToBase64(imgselectList.get(0).getPath())
        Bitmap b=Utils.base64ToBitmap(Utils.bitmapToBase64(bitmap));*/
  /*      ByteArrayOutputStream baos = new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes=baos.toByteArray();//bitmap转byte[]


        Glide.with(context).load(bytes).into(holder.img);//直接展示bitmap
*/
        holder.itemView.setTag(position);

    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {
          if (mListener!=null){
              mListener.onItemClick(view, list, (int)view.getTag());
          }

    }

    public  class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView img;
        public ViewHolder(View itemView) {
            super(itemView);
            img=(ImageView)itemView.findViewById(R.id.img);
        }
    }

    public  interface onRecyclerItemClickerListener {
        void onItemClick(View view, List<String> list, int i);
    }
}